package com.rightsidetech.http.controller.userApp.system;

import com.github.pagehelper.PageInfo;
import com.rightsidetech.commom.base.ResponseData;
import com.rightsidetech.commom.enums.CodeIdEnum;
import com.rightsidetech.commom.utils.BeanValidator;
import com.rightsidetech.http.aspect.Log;
import com.rightsidetech.http.aspect.Token;
import com.rightsidetech.http.base.BaseController;
import com.rightsidetech.system.dtos.request.FeedbackPageReq;
import com.rightsidetech.system.dtos.request.FeedbackSaveReq;
import com.rightsidetech.system.dtos.response.FeedbackPageResp;
import com.rightsidetech.system.dtos.response.HistoryFeedbackPageResp;
import com.rightsidetech.system.entity.SysFeedback;
import com.rightsidetech.system.service.SysFeedbackService;
import com.rightsidetech.trade.dtos.response.TdCommodityAppOrderAfterServiceResp;
import com.rightsidetech.trade.service.TdCommodityUserOrderService;
import com.rightsidetech.user.entity.UsUserLoginInfo;
import com.rightsidetech.user.service.UserInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shenyu.client.springmvc.annotation.ShenyuSpringMvcClient;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.ConstraintViolation;
import java.util.Date;

/**
 * @Author: yhr
 * @Date: 2021-12-16 14:12
 */
@RestController
@RequestMapping("/userApp/app_feedback")
@ShenyuSpringMvcClient(path = "/userApp/app_feedback/**")
@Api(tags = "意见反馈")
public class AppFeedbackController extends BaseController {
    @Resource
    private SysFeedbackService sysFeedbackService;
    @Resource
    private TdCommodityUserOrderService tdCommodityUserOrderService;
    @Resource
    private UserInfoService userInfoService;

    @Log
    @Token
    @GetMapping("/temp")
    @ApiOperation("反馈问题选项")
    public ResponseData temp() throws Exception {
        return sysFeedbackService.search();
    }

    @Log
    @Token
    @PostMapping("/saveFeedback")
    @ApiOperation("添加反馈问题")
    public ResponseData saveFeedback(@RequestBody FeedbackSaveReq req) throws Exception {
        ConstraintViolation validate = BeanValidator.validate(req);
        if (validate != null) {
            return new ResponseData(CodeIdEnum.ParameterMalformed.getValue(), validate.getMessage());
        }
        Long loginId = Long.parseLong(getAttributeForLoginId());
        String userPhone = userInfoService.findPhoneByNO(loginId);
        TdCommodityAppOrderAfterServiceResp userOrder = tdCommodityUserOrderService.selectByOrderNoAndUserInfoId(req.getOrderNo(),Long.parseLong(getAttribute()));
        if (ObjectUtils.isEmpty(userOrder)){
            return ResponseData.fail("未找到此订单");
        }
        SysFeedback sysFeedback = new SysFeedback();
        sysFeedback.setUserInfoId(Long.parseLong(getAttribute()));
        sysFeedback.setOrderNo(userOrder.getOrderNo());
        sysFeedback.setUserName(userOrder.getUsName());
        sysFeedback.setPhoneNo(userPhone);
        sysFeedback.setCreatedAt(new Date());
        sysFeedback.setQuestionId(req.getQuestionId());
        sysFeedback.setQuestionContent(req.getQuestionContent());
        return sysFeedbackService.saveFeedback(sysFeedback);
    }

    @Log
    @Token
    @GetMapping("historyPage")
    @ApiOperation("用户历史反馈列表")
    public ResponseData<PageInfo<HistoryFeedbackPageResp>> pageFeedback(FeedbackPageReq req){
        ConstraintViolation validate = BeanValidator.validate(req);
        if (validate != null) {
            return new ResponseData<>(CodeIdEnum.ParameterMalformed.getValue(), validate.getMessage());
        }
        Long userInfoId = Long.parseLong(getAttribute());
        return sysFeedbackService.historyPage(req,userInfoId);
    }
}
